home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / xconf / notice.c < prev    next >
C/C++ Source or Header  |  1994-08-17  |  1KB  |  62 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "xconf.h"
  4. #include "components.h"
  5.  
  6. /*
  7.     A notice is simply a table of string. It generally hold a message
  8.     to display to the user when a selection is made.
  9. */
  10. PUBLIC NOTICE::NOTICE ()
  11. {
  12.     tbstr = NULL;
  13.     nbstr = 0;
  14.     maxstr = 0;
  15. }
  16. PUBLIC NOTICE::~NOTICE ()
  17. {
  18.     for (int i=0; i<nbstr; i++) free (tbstr[i]);
  19.     free (tbstr);
  20. }
  21. PUBLIC void NOTICE::add (const char *str)
  22. {
  23.     if (maxstr == nbstr){
  24.         maxstr += 10;
  25.         tbstr = (char**)realloc (tbstr,maxstr*sizeof(char*));
  26.     }
  27.     tbstr[nbstr++] = strdup(str);
  28. }
  29.  
  30. PUBLIC int NOTICE::print(FILE *fout, int indent) const
  31. {
  32.     for (int i=0; i<nbstr; i++){
  33.         for (int n=0; n<indent; n++) fputc ('\t',fout);
  34.         fprintf (fout,"%s\n",tbstr[i]);
  35.     }
  36.     return 0;
  37. }
  38. /*
  39.     Place all the strings of a notice in a single one.
  40.     Return -1 if the maximum size of the buf is exceeded.
  41. */
  42. PUBLIC int NOTICE::format(char *buf, int maxsiz) const
  43. {
  44.     int ret = 0;
  45.     char *pt = buf;
  46.     for (int i=0; i<nbstr; i++){
  47.         char *str = tbstr[i];
  48.         int len = strlen(str);
  49.         if ((pt-buf)+len+1 >= maxsiz){
  50.             ret = -1;
  51.             break;
  52.         }else{
  53.             strcpy (pt,str);
  54.             pt += len;
  55.             *pt++ = '\n';
  56.         }
  57.     }
  58.     *pt = '\0';
  59.     return ret;
  60. }
  61.  
  62.